package com.tuozixuan.countdownlatch;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CountDownLatchDemo {

	public static void main(String[] args) throws InterruptedException {
		
		System.out.println("[Main]start>>>");
		List<Integer> list = Arrays.asList(1,2,3,4,5);
		ExecutorService executorService = Executors.newFixedThreadPool(3);
		CountDownLatch countDownLatch = new CountDownLatch(list.size());
		for (Integer i : list) {
			executorService.execute(() -> {
				try {
					System.out.println(String.format("[Thread%d]run...", i));
				} finally {
					countDownLatch.countDown();
				}
			});
		}
		System.out.println("[Main]count: " + countDownLatch.getCount());
		countDownLatch.await();
		System.out.println("[Main]end<<< count: " + countDownLatch.getCount());
	}
}
